import domtoimage from './dom-to-image';
import FileSaver from "file-saver"
import { message } from 'ant-design-vue'
import jsPDF from 'jspdf'

// 导出pdf
export const pageToPDF = async (elementId, title, bgColor) => {
  let node = document.getElementById(elementId);
			domtoimage.toJpeg(node, {
				width: node.clientWidth,
				height: node.clientHeight,
				cacheBust: true,
				style: {
					margin: 0,
					background: '#fff',
				}
			}).then(function (dataUrl) {
					let canvasWidth = node.clientWidth;
					 let canvasHeight = node.clientHeight;
					let imgObj = new Image()
					imgObj.src = dataUrl;
					document.documentElement.scrollTop = 0
					//待图片加载完后，将其显示在canvas上
					imgObj.onload = function (img) {
						let canvas = document.getElementById('myCanvas');
						canvas.width = node.clientWidth
						canvas.height = node.clientHeight
						canvas.getContext("2d").drawImage(imgObj, 0, 0, canvasWidth, canvasHeight); //将图片绘制到canvas中
						let contentWidth = node.clientWidth
						let contentHeight = node.clientHeight
						let pageHeight = contentWidth / 592.28 * 841.89
						let leftHeight = contentHeight
						let position = 0
						let imgWidth = 595.28
						let imgHeight = 592.28 / contentWidth * contentHeight
						let pageData = canvas.toDataURL('image/jpeg', 2.0)
						let PDF = new jsPDF('', 'pt', 'a4')
						if (leftHeight < pageHeight) {
							PDF.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight) // 大于1页高度时分页
						} else {
							while (leftHeight > 0) {
								PDF.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
								leftHeight -= pageHeight
								position -= 841.89
								if (leftHeight > 0) {
									PDF.addPage()
								}
							}
						}
						const aLink = document.createElement('a')
						document.body.appendChild(aLink)
						const url = window.URL.createObjectURL(toBlob(PDF.output('datauristring')));
						aLink.href = url
						let date = new Date()
						let year = date.getFullYear()
						let month = (date.getMonth() + 1).toString().padStart(2, 0)
						let day = date.getDate().toString().padStart(2, 0)
						let hour = date.getHours().toString().padStart(2, 0)
						let minutes = date.getMinutes().toString().padStart(2, 0)
						let seconds = date.getSeconds().toString().padStart(2, 0)
						aLink.download = title + '.pdf'
						aLink.click()
						window.URL.revokeObjectURL(url)
					}
					return dataUrl
				})
				.catch(function (error) {
					console.error('wrong!', error);
				});
}

export function toBlob(base64Data) {
  let byteString = base64Data
  if (base64Data.split(',')[0].indexOf('base64') >= 0) {
    byteString = atob(base64Data.split(',')[1]); // base64 解码
  } else {
    byteString = unescape(base64Data.split(',')[1]);
  }
  // 获取文件类型
  const mimeString = base64Data.split(';')[0].split(":")[1]; // mime类型

  // ArrayBuffer 对象用来表示通用的、固定长度的原始二进制数据缓冲区
  // let arrayBuffer = new ArrayBuffer(byteString.length) // 创建缓冲数组
  // let uintArr = new Uint8Array(arrayBuffer) // 创建视图

  const uintArr = new Uint8Array(byteString.length); // 创建视图

  for (let i = 0; i < byteString.length; i += 1) {
    uintArr[i] = byteString.charCodeAt(i);
  }
  // 生成blob
  const blob = new Blob([uintArr], {
    type: mimeString
  })
  // 使用 Blob 创建一个指向类型化数组的URL, URL.createObjectURL是new Blob文件的方法,可以生成一个普通的url,可以直接使用,比如用在img.src上
  return blob;
};

